package org.makcro.sms.services.chat.base.senders;

import java.text.MessageFormat;
import java.util.Hashtable;

import org.apache.log4j.Logger;
import org.makcro.sms.services.chat.base.service.EventManager;
import org.makcro.sms.services.chat.base.service.EventManager.EventParams;

import com.klikmee.sms.cef.CoreAdapter;
import com.klikmee.sms.cef.bulk.BulkSender;
import com.klikmee.sms.cef.bulk.SendBulkException;
import com.klikmee.sms.core.commons.events.AppEvent;
import com.klikmee.sms.core.commons.events.Dispatcher;

public class CoreBulkSenderImpl implements BulkSender {

	/**
	 * Logger instance
	 */
	private Logger		logger	= Logger.getLogger( CoreBulkSenderImpl.class );

	/**
	 * Core adapter is used to send messages
	 */
	private CoreAdapter	coreAdapter;

	/*
	 * Charging id for this bulk sender
	 */
	private Long		chargingId;
	
	/**
	 * Core service id for this bulk sender to use
	 */
	private Long serviceId;

	/**
	 * Instance of dispatcher used to dispatch bulk events
	 */
	private Dispatcher dispatcher;
	
	
	@Override
	public int send(Hashtable<BulkParams, Object> params) throws SendBulkException {
		// add charging info to bulk params
		params.put( BulkParams.CORE_CHARGING_ID, chargingId );
		params.put( BulkParams.CORE_SERVICE_ID, serviceId );
		// call actual bulk sending
		int bulkCoreId = coreAdapter.send( params );
	
		logger.info( MessageFormat.format( "Bulk message sent with reference id [{0}]", bulkCoreId ) );
		
		// announse bulk message sent event
		AppEvent event = new AppEvent( EventManager.BULK_MESSAGE_SENT );
		event.setData( EventParams.BULK_PARAMS, params );
		event.setData( EventParams.BULK_CORE_ID, (long)bulkCoreId );
		
		dispatcher.dispatch( event );
				
		return bulkCoreId;
	}

	public void setCoreAdapter(CoreAdapter coreAdapter) {
		this.coreAdapter = coreAdapter;
	}

	public Long getChargingId() {
		return chargingId;
	}

	public void setChargingId(Long chargingId) {
		this.chargingId = chargingId;
	}
	
	public void setServiceId(Long serviceId) {
		this.serviceId = serviceId;
	}
	
	public Long getServiceId() {
		return serviceId;
	}
	
	public void setDispatcher(Dispatcher dispatcher) {
		this.dispatcher = dispatcher;
	}
}
